package com.healthcloud.controller;

import com.healthcloud.dto.IntegralDto;
import com.healthcloud.entity.IntegralDetail;
import com.healthcloud.entity.IntegralMaster;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.Patient;
import com.healthcloud.interceptor.AuthorizationInterceptor;
import com.healthcloud.service.IntegralDetailService;
import com.healthcloud.service.IntegralMasterService;
import com.healthcloud.service.PatientService;
import com.healthcloud.service.TokenService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.IdcardInfoExtractorUtil;
import com.healthcloud.utils.JwtUtil;
import com.healthcloud.utils.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author zhangjie
 */
@Api("积分")
@RestController
@RequestMapping("/mp/integral")
public class IntegralController {

    @Autowired
    private IntegralMasterService integralMasterService;

    @Autowired
    private IntegralDetailService integralDetailService;

    @Autowired
    private PatientService patientService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "总积分")
    @GetMapping(value = "/totalScore")
    public ApiRes totalScore(HttpServletRequest request) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        IntegralMaster master = integralMasterService.findByUserId(userId.intValue());
        //没有则返回0积分
        if (master == null) {
            master = new IntegralMaster();
            master.setScore(0);
        }
        return new ApiRes(master);
    }

    @ApiOperation(value = "积分详情列表")
    @GetMapping(value = "/detailList")
    public MsgVo detailList(HttpServletRequest request, PageParam page) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        Integer masterId = integralMasterService.findByUserId(userId.intValue()).getId();
        int count = integralDetailService.countByMasterId(masterId);
        List list = integralDetailService.listByMasterId(masterId, page);
        return new MsgVo(list, count, count, page.getsEcho());

    }

    /**
     * 添加积分(不可重复)
     *
     * @param integralDto
     */
    @ApiIgnore
    @PostMapping(value = "/add")
    public void add(@RequestBody IntegralDto integralDto) {
        String idCard = integralDto.getIdCard();
        Patient patient = patientService.findByIdCard(idCard);
        //没有用户则不添加积分
        if (patient == null || patient.getUserId() == null) {
            return;
        }
        List<IntegralDetail> details = integralDetailService.queryBySnAndTestTime(integralDto.getSn(), integralDto.getTestTime());
        if (details != null && details.size() > 0) {
            return;
        }

        IntegralMaster master = integralMasterService.findByUserId(patient.getUserId());
        if (master == null) {
            master = new IntegralMaster();
            master.setScore(0);
            master.setUserId(patient.getUserId());
            integralMasterService.save(master);
        }

        IntegralDetail detail = new IntegralDetail();
        detail.setMsg(integralDto.getMsg());
        detail.setScore(integralDto.getScore());
        detail.setOperationDirection(integralDto.getOperationDirection());
        detail.setMasterId(master.getId());
        detail.setCreateTime(integralDto.getTestTime());
        detail.setSn(integralDto.getSn());
        integralDetailService.save(detail);

        //计算总分
        integralMasterService.score(master.getId());
    }


    @ApiOperation(value = "个人信息")
    @GetMapping(value = "/patient")
    public ApiRes patient(HttpServletRequest request) {
        Long userId = JwtUtil.getUserIdFromRequest(request);
        String idCard = JwtUtil.getIdCardFromRequest(request);
        Patient patient = patientService.findByIdCard(idCard);

        IdcardInfoExtractorUtil id = new IdcardInfoExtractorUtil(idCard);
        String sex = id.getGender();

        patient.setAge(id.getAge() + "");
        patient.setBirthday(id.getBirthday());
        patient.setGender(Integer.parseInt(sex));
        patientService.DataTimeToString(patient);
        return new ApiRes(patient);
    }

}
